package com.yz.design.stateDesign;

/**
 * 糖果机有四种状态 1.未投币 2，已投币 3.出售（已按转动糖果机） 4.已售完
 * 糖果机有四种动作 1.投币 3.退币 3.转动曲柄 4.发糖
 */
public class GumballMachine extends State {
    public State noQuarterState = new NoCoinState(this);
    public State hasQuarterState = new HasCoinState(this);
    public State soldState = new SoldState(this);
    public State soldOutState = new SoldOutState(this);

    /**
     * 糖果机状态 已售完
     */
    private State state = soldOutState;
    /**
     * 糖果机 糖果数量默认0
     */
    private int candyCount = 0;

    public GumballMachine(int count) {
        this.candyCount = count;
        //初始化 糖果数量>0  糖果机状态 未投币
        if (count > 0) {
            setState(noQuarterState);
        }
    }

    @Override
    public void insertCoin() {
        state.insertCoin();
    }

    @Override
    public void ejectCoin() {
        state.ejectCoin();
    }

    @Override
    public void turnCrank() {
        state.turnCrank();
    }

    @Override
    public void dispense() {
        state.dispense();
    }

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void setCandyCount(int candyCount) {
        this.candyCount = candyCount;
    }

    public int getCandyCount() {
        return candyCount;
    }

    public static void main(String[] args) {
        GumballMachine gumballMachine = new GumballMachine(1);
        gumballMachine.insertCoin();
        gumballMachine.turnCrank();
        gumballMachine.dispense();
        gumballMachine.ejectCoin();

    }

}